<template>
	<view class="lottery-container">
		<lottery2-title></lottery2-title>
		<lottery2-game ref="lotteryGame" :giftText="giftText" @play="onStartGetPrizeMsg" @resultDialogClose="onResultDialogClose"></lottery2-game>
		<lottery2-button @toHome="onToHome"  :info="info" :showTips="showTips"></lottery2-button>
	</view>
</template>

<script>
	import {
		getPrizeMsg,
		startGetPrizeMsg
	} from "../../api/index.js"
	export default {
		components: {},
		data() {
			return {
				msgList: [],
				pageType: null,
				lotteryType: 'sj',
				isDrawing: true,
				zhuan: 0,
				lotteryNums: 0,
				prizeList: [],
				info: {
					guize: ''
				},
				rule: '',
				id: '',
				list: [],
				bid: '',
				giftText: '',
				channel: "",
				isDrawLoading: false,
				innerAudioContext: null
			}
		},
		computed: {
			showTips() {
				var tips = "";
				if (this.channel) {
					switch (this.channel) {
						case "IOS":
							tips = "Apple Inc."
							break;
						case "HW":
							tips = "华为"
							break;
						case "OPPO":
							tips = "OPPO"
							break;
						case "VIVO":
							tips = "VIVO"
							break;
						case "MI":
							tips = "小米"
							break;
					}
					return `本次抽奖活动与设备生产商${tips}公司无关`;
				}
				return "";
			}
		},
		onShow() {
			
		},
		onBackPress(e) {
			if (options.from === 'navigateBack') {
				return false
			}
			if (e.from === 'backbutton') {
				try {
					this.$refs.lotteryGame.pause()
				} catch (e) {}
			
				uni.switchTab({
					url: getApp().globalData.HOME_PAGE
				})
			}
			return true
		},
		onHide() {
			try {
				this.$refs.lotteryGame.stop()
			} catch (e) {}
		},
		onUnload(){
			try {
				this.$refs.lotteryGame.stop()
			} catch (e) {}
		},
		onLoad(opt) {
			this.id = opt.id
			opt.pageType == 1 ? this.pageType = 1 : this.pageType = 2
			this.lotteryType = opt.lotteryType ? opt.lotteryType : 'sj'
			console.log(this.lotteryType)
			this.msgList = [this.pageType, this.id]
			this.onGetPrizeMsg()
			var channelVersionNumber = getApp().globalData.channelVersionNumber;
			var versionNumber = getApp().globalData.versionNumber;
			if (channelVersionNumber == versionNumber)
				this.channel = ""
			else
				this.channel = channelVersionNumber.replace("Public.", "").replace("." + versionNumber, "")
		},
		methods: {
			onToHome() {
				try {
					this.$refs.lotteryGame.pause()
				} catch (e) {}
				uni.switchTab({
					url: getApp().globalData.HOME_PAGE
				})
			},
			onResultDialogClose() {
				try {
					this.$refs.lotteryGame.pause()
				} catch (e) {}
				uni.switchTab({
					url: getApp().globalData.HOME_PAGE
				})
			},
			onBackPage() {
				if (this.bid != '') {
					uni.redirectTo({
						url: `/business/pages/business_tab/home?bid=${this.bid}`
					});
				} else {
					uni.switchTab({
						url: getApp().globalData.HOME_PAGE
					})
				}
			},
			async onGetPrizeMsg() {
				const res = await getPrizeMsg('s=/ApiChoujiang/index', {
					id: this.id
				})
				const {
					msg,
					data,
					status
				} = res.data
				if (status == 1) {
					this.list = data.jxarr.map(item => {
						return {
							name: item.mc,
							img: item.pic,
							tp: item.tp,
						}
					})
					this.prizeList = data.zjlist
					this.zhuan = data.zhuan
					this.info = data.info
					this.rule = data.info.guize
					this.bid = data.info.bid
					this.lotteryNums = data.remaindaytimes
					this.isDrawing = this.lotteryNums > 0
				} else {
					this.showToast(msg)
				}
			},
			async onStartGetPrizeMsg() {
				if (this.isDrawLoading) {
					return;
				}
				this.isDrawLoading = true;
				if (this.lotteryNums == 0) {
					this.showToast('暂无抽奖次数')
					return
				}
				uni.showLoading({
					title: '加载数据中...'
				})
				const res = await startGetPrizeMsg('s=/ApiChoujiang/index', {
					type: this.pageType,
					id: this.id,
					op: 'getjx'
				})
				const {
					status,
					msg,
					data
				} = res.data
				uni.hideLoading()
				if (status == 1) {
					this.playReward(data.jxindex, data.jx, data.text)
				} else {
					this.showToast(msg)
					this.isDrawLoading = false;
				}
			},
			playReward(idx, isRate, text) {
				let that = this;
				var awardIndex = idx
				this.$refs.lotteryGame.playGame(awardIndex)
				if (that.lotteryNums != -1) {
					that.lotteryNums = that.lotteryNums - 1;
				}
				that.isDrawing = that.lotteryNums > 0
				that.giftText = text
			}
		}
	}
</script>

<style lang="less" scoped>
  .lottery-container {
    min-height: 100vh;
    background: url('https://wx.2024csnl.com/assets/lottery2/lottery2-bj.jpg') center top no-repeat #f57453;
    background-size: 100% auto;
  }
</style>